package com.ly.juc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Classname ListUnSafe
 * @Description TODO
 * @Date 2020/10/4 15:05
 * @Author 冷心影翼
 */
public class ListUnSafe {

	public static void main(String[] args) {
		unsafeInsert();
//		collectionsSync();
//		copyOnWriteList();
	}

	public static void unsafeInsert() {
		List list = new ArrayList();
		for(int i=0;i<1000;i++) {
			int finalI = i;
			new Thread(()->{
				list.add(finalI);
			}).start();
			System.out.println(list);
		}

	}

	public static void collectionsSync() {
		List list = Collections.synchronizedList(new ArrayList<>());
		for(int i=0;i<100;i++) {
			int finalI = i;
			new Thread(()->{
				list.add(finalI);
			}).start();
			System.out.println(list);
		}

	}

	public static void copyOnWriteList() {
		List list = new CopyOnWriteArrayList();
		for(int i=0;i<100;i++) {
			int finalI = i;
			new Thread(()->{
				list.add(finalI);
			}).start();
		}
		System.out.println(list);
	}
}
